// 分析mediainfo xml
export default class MediaInfo {
  static parseMediainfo(xml) {
    if (xml === undefined || xml == null || xml.length === 0) {
      return null
    }
    const mediainfo = {}
    const parser = new DOMParser()
    const xmlDoc = parser.parseFromString(xml, 'text/xml')
    // const rootNode = xmlDoc.getRootNode;
    // if (rootNode != null) {
    const { childNodes, attributes } = xmlDoc.firstChild
    for (let j = 0; j < attributes.length; j += 1) {
      if (attributes[j].nodeName === 'name') {
        mediainfo.fileName = attributes[j].nodeValue
      }
    }
    for (let i = 0; i < childNodes.length; i += 1) {
      const node = childNodes[i]
      if (node.nodeName === 'container') {
        mediainfo.container = node.textContent
      } else if (node.nodeName === 'size') {
        mediainfo.size = parseInt(node.textContent, 10)
      } else if (node.nodeName === 'OverallBitrate_Mode') {
        mediainfo.overallBitrateMode = node.textContent
      } else if (node.nodeName === 'programs') {
        mediainfo.programs = MediaInfo.parsePrograms(node)
      } else if (node.nodeName === 'video') {
        mediainfo.video = MediaInfo.parseVideo(node)
      } else if (node.nodeName === 'subtitle') {
        mediainfo.audio = MediaInfo.parseSubtitle(node)
      } else if (node.nodeName === 'duration') {
        mediainfo.duration = parseInt(node.textContent, 10)
      } else if (node.nodeName === 'duration') {
        mediainfo.duration = parseInt(node.textContent, 10)
      }
    }
    // }
    return mediainfo
  }

  static parsePrograms(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const programs = []
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      const nodeChild = childNodes[i]
      if (nodeChild.nodeName === 'program') {
        const program = MediaInfo.parseProgram(nodeChild)
        if (program !== undefined) {
          programs.push(program)
        }
      }
    }

    return programs
  }

  static parseProgram(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const program = {}
    if (node.hasAttributes) {
      const { attributes } = node
      for (let j = 0; j < attributes.length; j += 1) {
        if (attributes[j].nodeName === 'idx') {
          program.pid = attributes[j].nodeValue
        }
      }
    }
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      const nodeChild = childNodes[i]
      if (nodeChild.nodeName === 'name') {
        program.name = nodeChild.textContent
      } else if (nodeChild.nodeName === 'used') {
        program.used = nodeChild.textContent
      } else if (nodeChild.nodeName === 'videos') {
        program.videos = MediaInfo.getProgramVideos(nodeChild)
      } else if (nodeChild.nodeName === 'audios') {
        program.audios = MediaInfo.getProgramAudios(nodeChild)
      } else if (nodeChild.nodeName === 'subtitles') {
        program.subtitles = MediaInfo.getProgramSubtitles(nodeChild)
      }
    }
    return program
  }

  static getProgramVideos(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const videos = []
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      if (childNodes[i].nodeName === 'video') {
        const video = MediaInfo.parseVideo(childNodes[i])
        if (video !== undefined) {
          videos.push(video)
        }
      }
    }

    return videos
  }

  static getProgramAudios(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }

    const audios = []
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      if (childNodes[i].nodeName === 'audio') {
        const audio = MediaInfo.parseAudio(childNodes[i])
        if (audio !== undefined) {
          audios.push(audio)
        }
      }
    }

    return audios
  }

  static getProgramSubtitles(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const subtitles = []
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      if (childNodes[i].nodeName === 'subtitle') {
        const subtitle = MediaInfo.parseSubtitle(childNodes[i])
        if (subtitle !== undefined) {
          subtitles.push(subtitle)
        }
      }
    }

    return subtitles
  }

  static parseVideo(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const video = {}
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      const child = childNodes[i]
      if (child.nodeName === 'pid') {
        video.pid = child.textContent
      } else if (child.nodeName === 'name') {
        video.name = child.textContent
      } else if (child.nodeName === 'used') {
        video.used = child.textContent
      } else if (child.nodeName === 'codec') {
        video.codec = MediaInfo.getVideoCodec(child.textContent)
      } else if (child.nodeName === 'duration') {
        video.duration = parseInt(child.textContent, 10)
      } else if (child.nodeName === 'bitrate') {
        const bitrate =
          parseInt(child.textContent.replace('kbps', ''), 10) * 1000
        video.bitrate = bitrate
      } else if (child.nodeName === 'framerate') {
        video.framerate = child.textContent
      } else if (child.nodeName === 'resolution') {
        const resolution = child.textContent.split('x')
        if (resolution !== undefined && resolution.length === 2) {
          video.width = parseInt(resolution[0], 10)
          video.height = parseInt(resolution[1], 10)
        }
      } else if (child.nodeName === 'aspect_ratio') {
        const aspect = child.textContent.split(':')
        video.aspectX = parseInt(aspect[0], 10)
        video.aspectY = parseInt(aspect[1], 10)
      } else if (child.nodeName === 'Rotation') {
        video.rotation = child.textContent
      } else if (child.nodeName === 'VideoBitrate_Mode') {
        video.bitrateMode = child.textContent
      }
    }
    return video
  }

  static parseAudio(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const audio = {}
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      const child = childNodes[i]
      if (child.nodeName === 'pid') {
        audio.pid = child.textContent
      } else if (child.nodeName === 'name') {
        audio.name = child.textContent
      } else if (child.nodeName === 'language') {
        audio.language = child.textContent
      } else if (child.nodeName === 'used') {
        audio.used = child.textContent
      } else if (child.nodeName === 'codec') {
        audio.codec = MediaInfo.getAudioCodec(child.textContent)
      } else if (child.nodeName === 'subcodec') {
        audio.subcodec = child.textContent
      } else if (child.nodeName === 'duration') {
        audio.duration = child.textContent
      } else if (child.nodeName === 'bitrate') {
        const bitrate =
          parseInt(child.textContent.replace('kbps', ''), 10) * 1000
        audio.bitrate = bitrate
      } else if (child.nodeName === 'channel') {
        audio.channel = child.textContent
      } else if (child.nodeName === 'samplerate') {
        const samplerate =
          parseInt(child.textContent.replace('kHz', ''), 10) * 1000
        audio.samplerate = samplerate
      } else if (child.nodeName === 'bitdepth') {
        audio.bitDepth = child.textContent
      } else if (child.nodeName === 'Rotation') {
        audio.rotation = child.textContent
      } else if (child.nodeName === 'AudioBitrate_Mode') {
        audio.bitrateMode = child.textContent
      }
    }
    return audio
  }

  static parseSubtitle(node) {
    if (
      node === undefined ||
      node.childNodes === undefined ||
      node.childNodes.length === 0
    ) {
      return undefined
    }
    const subtitle = {}
    const { childNodes } = node
    for (let i = 0; i < childNodes.length; i += 1) {
      const child = childNodes[i]
      if (child.nodeName === 'pid') {
        subtitle.pid = child.textContent
      } else if (child.nodeName === 'name') {
        subtitle.name = child.textContent
      } else if (child.nodeName === 'language') {
        subtitle.language = child.textContent
      } else if (child.nodeName === 'used') {
        subtitle.used = child.textContent
      }
    }

    return subtitle
  }

  static getVideoCodec(val) {
    let ret = val
    if (val === 'H.264') {
      ret = 'H264'
    } else if (val === 'HEVC') {
      ret = 'H265'
    } else if (val === 'MPEG-2') {
      ret = 'MPEG2'
    }

    return ret
  }

  static getAudioCodec(val) {
    let ret = val
    if (val === 'MPEG Audio L2') {
      ret = 'MP2'
    } else if (val === 'MPEG Audio L3') {
      ret = 'MP3'
    }

    return ret
  }
}
